// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: Implementation of CImplActionClose.
//   $Id$




#include "stdafx.h"
#include "ActionClose.h"
#include "XFormsElementClose.h"
#include "XFormsXFormsElement.h"

CImplActionClose::CImplActionClose(CXFormsElementClose * pXFormsElement)
:CImplAction(reinterpret_cast<CXFormsElementAction *>(pXFormsElement))
{
}

CImplActionClose::~CImplActionClose()
{
}

HRESULT CImplActionClose::PerformAction()
{
	HRESULT hr = E_PENDING;
	if(m_pXForms) 
	{
		//TODO: Move to a front-end class
		//	As this is an artefact of using IE, this should really be concealed by a front-end class,
		//	thereby allowing CImplActionClose to appear fully platform independent.
		//Ironically, this might be best handled by XFormsXFormsElement.
		CComPtr<IHTMLDocument2> pDoc;
		hr = m_pXForms->GetOwnerDocument(&pDoc);
		if(pDoc) 
		{
			CComPtr<IHTMLWindow2> pWin;
			hr = pDoc->get_parentWindow(&pWin);
			if(pWin)
			{
				//HACK: prevent IE from showing annoying dialog.
				//If an IE window attempts to close itself, and it hasn't been opened by another window
				//	(e.g. as a pop-up), a dialog will appear, stating that the web page you are viewing
				//	is attempting to close the window.  
				//This is, (to paraphrase Msr. Dupont) very boring for the user.
				//	A workaround to this, is to set the opener property of the current window.

				CComVariant vt;
				hr = pWin->get_opener(&vt);
				if(VT_EMPTY == vt.vt)
				{
					vt = CComVariant(L"dummyOpener");
					pWin->put_opener(vt);
				}
				hr = pWin->close();
			}
		}
	}
	return hr;
}
